<template>
  <div :id="id" :style="{height:height,width:width}"/>
</template>

<script>
import * as echarts from 'echarts'

export default {
  name: "ChartDesign",
  props: {
    id: {
      type: String,
      default: 'id'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '300px'
    },
    chartData: {
      type: Array,
      default: () => {
        return []
      }
    }
  },
  data() {
    return {
      chart: null
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
      window.addEventListener('resize', () => {
        if (this.chart) {
          this.chart.resize()
        }
      })
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
    window.removeEventListener('resize', () => {
      this.chart.resize()
    })
  },
  methods: {
    initChart() {
      // 表格配对象
      const option = {
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'shadow'
          }
        },
        grid: {
          left: '5%',
          bottom: '6%',
          width: '92%',
          height: '75%'
        },
        legend: {
          data: ['需求数', '成品素材', '制作用时', '原创率']
        },
        // toolbox: {
        //   show: true,
        //   orient: 'vertical',
        //   left: 'right',
        //   top: 'center',
        //   feature: {
        //     mark: { show: true },
        //     dataView: { show: true, readOnly: false },
        //     magicType: { show: true, type: ['line', 'bar', 'stack'] },
        //     restore: { show: true },
        //     saveAsImage: { show: true }
        //   }
        // },
        dataset: {
          dimensions: ['userName', 'requestCount', 'creativeCount', 'makeTime', 'originateRate'],
          source: this.chartData
        },
        xAxis: [
          {
            type: 'category',
            axisTick: {show: false},
          }
        ],
        yAxis: [
          {}
        ],
        series: [
          {
            name: '需求数',
            type: 'bar',
            barGap: 0,
            emphasis: {
              focus: 'series'
            },
          },
          {
            name: '成品素材',
            type: 'bar',
            emphasis: {
              focus: 'series'
            },
          },
          {
            name: '制作用时',
            type: 'bar',
            emphasis: {
              focus: 'series'
            },
          },
          {
            name: '原创率',
            type: 'bar',
            emphasis: {
              focus: 'series'
            },
          }
        ]
      }
      this.chart = echarts.init(document.getElementById(this.id))
      this.chart.setOption(option)
    }

  }
}
</script>

<style lang="scss" scoped>

</style>
